package ru.fivestarter.multileveledcache.strategy;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import ru.fivestarter.multileveledcache.cache.Cache;

public class LRU implements Cache {
    private Cache cache;
    private Counter counter;;

    public LRU(Cache cache,int size) {
        this.cache=cache;
        this.counter=new LRUCounter(size);
    }

    @Override
    public Object find(String key) {
        Object result=cache.find(key);
        if(result!=null){
            counter.refresh(System.nanoTime(),key);
        }
        return result;
    }

    @Override
    public Map.Entry<String, Object> add(String key, Object value) {
        cache.add(key, value);
        String pushKey=counter.add(System.nanoTime(),key);
        if(pushKey!=null){
            return cache.remove(pushKey);
            
        }
        return null;
    }

    @Override
    public Map.Entry remove(String key) {
        
        counter.remove(key);
        return cache.remove(key);
    }

    @Override
    public void clear() {
        cache.clear();
        counter.clear();
    }
}


